# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=cmake
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.20.2
pkgrel=1
pkgdesc="A cross-platform open-source make system (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.cmake.org/"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-bzip2"
             "${MINGW_PACKAGE_PREFIX}-ncurses"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-xz"
             "${MINGW_PACKAGE_PREFIX}-zstd"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || \
               [[ ${MINGW_PACKAGE_PREFIX} == *-ucrt-* ]] || \
               echo "${MINGW_PACKAGE_PREFIX}-emacs" \
                    "${MINGW_PACKAGE_PREFIX}-qt5"))

depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-pkg-config"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libarchive"
         "${MINGW_PACKAGE_PREFIX}-libuv"
         "${MINGW_PACKAGE_PREFIX}-rhash"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-qt5: CMake Qt GUI"
            "${MINGW_PACKAGE_PREFIX}-emacs: for cmake emacs mode")
options=('staticlibs') # '!strip' 'debug'
source=("https://github.com/Kitware/CMake/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-Disable-response-files-for-MSYS-Generator.patch"
        "0002-Do-not-install-Qt-bundle-in-cmake-gui.patch"
        "0003-pkg-config-Add-dont-define-prefix-when-PKG_CONFIG_WI.patch"
        "0004-Output-line-numbers-in-callstacks.patch")
sha256sums=('aecf6ecb975179eb3bb6a4a50cae192d41e92b9372b02300f9e8f1d5f559544e'
            '25793edcbac05bb6d17fa9947b52ace4a6b5ccccf7758e22ae9ae022ed089061'
            'f6cf6a6f2729db2b9427679acd09520af2cd79fc26900b19a49cead05a55cd1a'
            '969b666b138338ca69be6668410b523ee1180cb163561f063b9d7764dc34d613'
            '15fdf3fb1a0f1c153c8cfbdd2b5c64035b7a04de618bfe61d7d74ced0d6a5c4b')


# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #


prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    0001-Disable-response-files-for-MSYS-Generator.patch \
    0002-Do-not-install-Qt-bundle-in-cmake-gui.patch \
    0003-pkg-config-Add-dont-define-prefix-when-PKG_CONFIG_WI.patch \
    0004-Output-line-numbers-in-callstacks.patch
}

build() {
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  local -a _qtconfig
  case "${MINGW_PACKAGE_PREFIX}" in
    *-clang-*|*-ucrt-*)
      ;;
    *)
      _qtconfig+=("--qt-gui" "--qt-qmake=${MINGW_PREFIX}/bin/qmake.exe")
      ;;
  esac

  MSYSTEM=MINGW FC=${MINGW_PREFIX}/bin/gfortran.exe \
    "${srcdir}"/${_realname}-${pkgver}/configure  \
    --prefix=${MINGW_PREFIX}                      \
    --system-libs                                 \
    --parallel=${NUMBER_OF_PROCESSORS}            \
    --mandir=share/man                            \
    --docdir=share/doc/cmake                      \
    --sphinx-man --sphinx-html                    \
    --bootstrap-system-jsoncpp                    \
    --bootstrap-system-libuv                      \
    --bootstrap-system-librhash                   \
    ${_qtconfig[@]}

  plain "Start building..."
  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  ./bin/ctest.exe -j$(($(nproc)+1))
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  ./bin/cmake.exe \
     -DCMAKE_INSTALL_PREFIX:PATH=${pkgdir}${MINGW_PREFIX} \
     -DCMAKE_INSTALL_LOCAL_ONLY:BOOL=OFF -P cmake_install.cmake
  install -d "${pkgdir}${MINGW_PREFIX}"/share/emacs/site-lisp/

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && \
     [[ ${MINGW_PACKAGE_PREFIX} != *-ucrt-* ]]; then
    ${MINGW_PREFIX}/bin/emacs -batch -f batch-byte-compile \
      "${pkgdir}${MINGW_PREFIX}"/share/emacs/site-lisp/cmake-mode.el
  fi
}
